package com.techstar.common.jackson;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.techstar.common.utils.StrToDateTimeUtil;

public class StrDateDeserializer extends StdDeserializer<String>{
	private static final long serialVersionUID = 1L;
	
	public StrDateDeserializer() {
        this(String.class);
    }

    protected StrDateDeserializer(Class<?> type) {
        super(type);
    }
	
	@Override
	public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException{
		if (StringUtils.isBlank(jp.getText())||"null".equals(jp.getText().toLowerCase())) {
			return "空";
		}else {
			DateTime date = StrToDateTimeUtil.strToDateTime(jp.getText());
			return date.toString("yyyy-MM-dd");
		}
	}
	public static void main(String[] args) throws JsonProcessingException, IOException {
		DateTime date = StrToDateTimeUtil.strToDateTime("2021-10-14 11:15:00");
		System.out.println(date.toString("yyyy-MM-dd"));
	}
}